﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>鼠标进入后才允许拖动</title>
  <style>
    body {
      font-family: 'Arial', sans-serif;
      margin: 0;
      padding: 20px;
      background-color: #f5f5f5;
    }
    
    .container {
      max-width: 800px;
      margin: 0 auto;
    }
    
    .draggable-area {
      position: relative;
      width: 300px;
      height: 200px;
      margin: 20px auto;
      border: 2px dashed #ccc;
      border-radius: 8px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      background-color: white;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      transition: all 0.3s ease;
    }
    
    .draggable-area.active {
      border-color: #4a90e2;
      background-color: #f0f7ff;
    }
    
    .drag-handle {
      display: none;
      padding: 8px 16px;
      background-color: #4a90e2;
      color: white;
      border-radius: 4px;
      cursor: move;
      margin-bottom: 10px;
      transition: background-color 0.2s;
    }
    
    .draggable-area.active .drag-handle {
      display: block;
    }
    
    .drag-handle:hover {
      background-color: #357abd;
    }
    
    .status-message {
      text-align: center;
      color: #666;
      font-size: 14px;
    }
    
    .draggable-area.active .status-message {
      color: #4a90e2;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <div class="container">
    <h3>鼠标进入区域后才允许拖动</h3>
    
    <div class="draggable-area" id="draggableArea">
      <div class="drag-handle">拖动我</div>
      <div class="status-message">将鼠标移入此区域以启用拖动</div>
    </div>
    
    <div class="info">
      <p>提示：只有当鼠标进入蓝色边框区域时，才能拖动这个盒子。</p>
      <p>拖动手柄会在鼠标进入区域后显示。</p>
    </div>
  </div>

  <script>
    // 获取DOM元素
    const draggableArea = document.getElementById('draggableArea');
    let isDragging = false;
    let currentX;
    let currentY;
    let initialX;
    let initialY;
    let scaleX = 1;
    let scaleY = 1;
    
    // 鼠标进入区域时激活拖动功能
    draggableArea.addEventListener('pointerenter', () => {
      draggableArea.classList.add('active');
      console.log('鼠标进入区域，允许拖动');
    });
    
    // 鼠标离开区域时禁用拖动功能
    draggableArea.addEventListener('pointerleave', () => {
      draggableArea.classList.remove('active');
      isDragging = false;
      console.log('鼠标离开区域，禁用拖动');
    });
    
    // 拖动相关事件监听
    draggableArea.addEventListener('mousedown', dragStart);
    document.addEventListener('mousemove', drag);
    document.addEventListener('mouseup', dragEnd);
    document.addEventListener('mouseleave', dragEnd);
    
    // 开始拖动
    function dragStart(e) {
      // 只有当区域处于激活状态时才允许拖动
      if (!draggableArea.classList.contains('active')) {
        return;
      }
      
      // 获取鼠标位置和元素初始位置
      initialX = e.clientX - draggableArea.offsetLeft;
      initialY = e.clientY - draggableArea.offsetTop;
      
      isDragging = true;
      draggableArea.style.transition = 'none';
      
      // 添加拖动中的样式
      draggableArea.classList.add('dragging');
    }
    
    // 拖动中
    function drag(e) {
      if (!isDragging) return;
      
      e.preventDefault();
      
      // 计算新位置
      currentX = e.clientX - initialX;
      currentY = e.clientY - initialY;
      
      // 应用新位置
      draggableArea.style.left = currentX + 'px';
      draggableArea.style.top = currentY + 'px';
    }
    
    // 结束拖动
    function dragEnd() {
      isDragging = false;
      draggableArea.style.transition = 'all 0.3s ease';
      draggableArea.classList.remove('dragging');
    }
  </script>
</body>
</html>